﻿using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks .Data ;

namespace AnyOffice.general.report.info
{
    public partial class user_report : ModulePage
    {
        private void BindReportData()
        {
            DataTable table = new ReportDao().SelectTop10(base.Conn, this.ViewState["UserId"].ToString());
            if (table.Rows.Count > 0)
            {
                this.pnlMain.Visible = true;
                this.rptReport.DataSource = table;
                this.rptReport.DataBind();
            }
            else
            {
                this.litReport.Text = base.ShowMessage("无月报");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.txtBeginDate.Value = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1";
                this.txtEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                string userId = (Request.QueryString["user_id"] == null) ? "" : Request.QueryString["user_id"].ToString();
                this.ViewState["UserId"] = userId;
                if (!SecurityManager.IsCanManage(userId))
                {
                    base.CloseConn();
                    Response.Write(base.ShowErrorNoNav("无权限"));
                    return;
                }
                DataTable table = new UsersDao().SelectByUserId(base.Conn, userId);
                if (table.Rows.Count > 0)
                {
                    this.litUserName.Text = table.Rows[0]["UserName"].ToString();
                }
                this.BindReportData();
            }
            if (this.Page.IsPostBack)
            {
                this.pnlDiary.Visible = false;
                this.pnlMain.Visible = false;
                this.pnlResult.Visible = true;
                ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT TOP 10 ");
                builder.Append("\t[Report].[ReportId], ");
                builder.Append("\t[Report].[Title], ");
                builder.Append("\t[Report].[Type], ");
                builder.Append("\t[Report].[ReportTime], ");
                builder.Append("\t[Users].[UserName], ");
                builder.Append("\t[Department].[DeptName] ");
                builder.Append("FROM ");
                builder.Append("\t[dbo].[Report], ");
                builder.Append("\t[dbo].[Users], ");
                builder.Append("\t[dbo].[Department] ");
                builder.Append("WHERE ");
                builder.Append("\t[Report].[UserId] = [Users].[UserId] ");
                builder.Append("\tAND [Users].[DeptId] = [Department].[DeptId] ");
                builder.Append("    AND Report.UserId = '" + this.ViewState["UserId"].ToString() + "' ");
                if (this.txtBeginDate.Value != string.Empty)
                {
                    builder.Append(" AND Report.ReportTime >= @BeginDate ");
                    list.Add(new object[] { "@BeginDate", Convert.ToDateTime(this.txtBeginDate.Value + " 00:00:00") });
                }
                if (this.txtEndDate.Value != string.Empty)
                {
                    builder.Append(" AND Report.ReportTime <= @EndDate ");
                    list.Add(new object[] { "@EndDate", Convert.ToDateTime(this.txtEndDate.Value + " 23:59:59") });
                }
                if (this.txtKey.Value != string.Empty)
                {
                    builder.Append(" AND (Report.Title LIKE '%' + @Key + '%' OR Report.Note1 LIKE '%' + @Key + '%' OR Report.Note2 LIKE '%' + @Key + '%' OR Report.Note3 LIKE '%' + @Key + '%' OR Report.Note4 LIKE '%' + @Key + '%') ");
                    list.Add(new object[] { "@Key", this.txtKey.Value });
                }
                builder.Append(" ORDER BY Report.ReportTime DESC");
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray4 = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray4[0].ToString(), objArray4[1]);
                }
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , builder.ToString(), commandParameters).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.rptQuery.DataSource = table2;
                    this.rptQuery.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的月报");
                }
            }
        }


        /// <summary>
        /// 根据类型编号，得到类型名称
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public string getTypeNameByTypeID(string typeid)
        {
            string returnstr = string.Empty;

            if (typeid == "1")
            {
                returnstr = "月汇报";

            }
            else if (typeid == "2")
            {
                returnstr = "季汇报";

            }
            else if (typeid == "3")
            {
                returnstr = "年汇报";

            }

            return returnstr;

        }

        /// <summary>
        /// 根据类型编号，得到类型名称
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public string getJsNameByTypeID(string typeid)
        {
            string returnstr = string.Empty;

            if (typeid == "1")
            {
                returnstr = "open_month";

            }
            else if (typeid == "2")
            {
                returnstr = "open_week";

            }
            else if (typeid == "3")
            {
                returnstr = "open_year";

            }

            return returnstr;

        }
 

    }
}
